package ai.leinao.notify.core;

import ai.leinao.notify.core.enums.NotifyType;
import ai.leinao.notify.core.configuration.ResourceNotifyProperties;
import ai.leinao.notify.core.provider.NotifierProvider;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;

/**
 * 基于配置文件形式的通知管理器
 *
 * @author zhong.wang
 * @date 2025/7/8
 */
@Getter
@Setter
public class ResourceNotifierRegistry extends AbstractNotifierRegistry implements SmartInitializingSingleton {
    /**
     * 资源模板管理器
     */
    private final ResourceNotifyProperties resourceNotifyProperties;

    /**
     * Spring上下文
     */
    private final ApplicationContext applicationContext;

    /**
     * 构造函数
     * @param resourceNotifyProperties
     */
    public ResourceNotifierRegistry(ResourceNotifyProperties resourceNotifyProperties, ApplicationContext applicationContext) {
        this.resourceNotifyProperties = resourceNotifyProperties;
        this.applicationContext = applicationContext;
    }
    /**
     * 根据通知类型和通知器ID获取通知属性
     *
     * @param notifyType
     * @param id
     * @return
     */
    @Override
    protected NotifyProperties getNotifyProperties(NotifyType notifyType, String id) {
        return resourceNotifyProperties.getNotifyProperties(notifyType.getType(), id);
    }

    /**
     * 初始化，获取所有NotifierProvider并注册
     */
    @Override
    public void afterSingletonsInstantiated() {
        applicationContext.getBeanProvider(NotifierProvider.class)
                .forEach(this::registerProvider);
    }
}
